package com.kuanter.kuanterauto.setting;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.umeng.analytics.MobclickAgent;

public class SettingFeedbackActivity   extends BaseActivity_NoBar{

	private Button btTopLeft_feedback=null;
	private Button btTopRight_feedback=null;
	private EditText et_feedback = null;
	private TextView hasnumTV = null;// 用来显示剩余字数
    int num = 200;
    DataLoader dataLoader=null;
    
    Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			finishProgress();
			switch (msg.what) {
			case 1:
				Bundle bundle = msg.getData();
				String json = bundle.getString("result");
//				System.out.println("---------------------" + json);
				Toast.makeText(SettingFeedbackActivity.this,
						"发送成功", Toast.LENGTH_SHORT)
						.show();
				break;
			case -1:
				Toast.makeText(SettingFeedbackActivity.this,
						"网络连接错误，请稍后重试", Toast.LENGTH_SHORT)
						.show();
				break;
			}
//			finish();
			
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_feedback);
		
		dataLoader=new DataLoader(this);
		initView();
		initListener();
		
		hasnumTV = (TextView) findViewById(R.id.tv_feedback);
        hasnumTV.setText("0/200");
        Timer timer = new Timer(); //设置定时器
        timer.schedule(new TimerTask() {
        @Override
        	public void run() { //弹出软键盘的代码
        		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        		imm.showSoftInput(et_feedback, 0);
        	}
        }, 300); //设置300毫秒的时长
        
        et_feedback.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            public void onTextChanged(CharSequence s,int start, int before, int count) {
                temp = s;
            }
            public void afterTextChanged(Editable s) {
                hasnumTV.setText(s.length()+"/200");
                selectionStart = et_feedback.getSelectionStart();
                selectionEnd = et_feedback.getSelectionEnd();
                if (temp.length() > num) {
                    s.delete(selectionStart - 1,selectionEnd);
                    int tempSelection = selectionEnd;
                    et_feedback.setText(s);
                    et_feedback.setSelection(tempSelection);//设置光标在最后
                }
            }
			
        });
	}
	
	private void initView(){
		btTopLeft_feedback=(Button) findViewById(R.id.btTopLeft_feedback);
		btTopRight_feedback=(Button) findViewById(R.id.btTopRight_feedback);
		et_feedback=(EditText) findViewById(R.id.et_feedback);
		
	}
	
	private void initListener(){
		btTopLeft_feedback.setOnClickListener(this);
		btTopRight_feedback.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btTopLeft_feedback:
			finish();
			break;
		case R.id.btTopRight_feedback:
			sendFeedback(et_feedback.getText().toString());
			break;
		}
	}
	
	private void sendFeedback(String s)
	{
		if(s.length()>0)
		{
			List<NameValuePair> p = new ArrayList<NameValuePair>();
			SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
			p.add(new BasicNameValuePair("feedbackMsg", s));
//			HttpTask task = new HttpTask(mHandler,p,this);
//			task.execute(MainConstants.HOST + "sendFeedback");// url
			showProgress("正在发送您的反馈信息...");
			dataLoader.DisplayData(MainConstants.HOST + "sendFeedback", p, mHandler, "", false);
		}
		else
		{
			Toast.makeText(SettingFeedbackActivity.this,
					"请输入反馈信息", Toast.LENGTH_SHORT)
					.show();
		}
		
		
	}
	
	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

}
